package cn.jingyuan.swan.system.service.impl;

import cn.jingyuan.swan.cloud.core.WarnExceptionUtils;
import cn.jingyuan.swan.system.domain.enums.PersistEnum;
import cn.jingyuan.swan.system.domain.sys.dao.ApiDao;
import cn.jingyuan.swan.system.domain.sys.entity.Api;
import cn.jingyuan.swan.system.enums.SysApiCategoryEnum;
import cn.jingyuan.swan.system.enums.SysAuthorityTypeEnum;
import cn.jingyuan.swan.system.service.ApiService;
import cn.jingyuan.swan.system.service.AuthorityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ApiServiceImpl implements ApiService {

    @Resource
    ApiDao apiDao;

    @Resource
    AuthorityService authorityService;

    @Override
    public Long addApi(Api api) {
        String apiCode = api.getApiCode();
        if (this.isExist(apiCode)) {
            log.error("接口地址编码已存在 , apiCode={}", apiCode);
            throw WarnExceptionUtils.newInstance(String.format("接口地址编码已存在"));
        }

        if (null == api.getPriority()) {
            api.setPriority(0);
        }

        if (null == api.getApiCategory()) {
            api.setApiCategory(SysApiCategoryEnum.DEFAULT);
        }

        if (null == api.getIsAuth()) {
            api.setIsAuth(0);
        }

        Date currentTime = new Date();

        api.setInvalidStatus(true);
        api.setCreateTime(currentTime);
        api.setUpdateTime(currentTime);

        apiDao.insert(api);

        Long apiId = api.getApiId();

        authorityService.saveOrUpdateAuthority(apiId, SysAuthorityTypeEnum.API);

        return apiId;
    }

    @Override
    public void removeApi(Long apiId) {
        Api api = this.getApi(apiId);
        if (null != api && PersistEnum.YES == api.getIsPersist()) {
            log.error("保留数据, 不允许删除, apiId={}", apiId);
            throw WarnExceptionUtils.newInstance("保留数据，不允许删除！");
        }

        authorityService.removeAuthority(apiId, SysAuthorityTypeEnum.API);
        apiDao.delete(apiId);
    }

    @Override
    public void updateApi(Api api) {
        Api savedApi = getApi(api.getApiId());
        if (savedApi == null) {
            log.error("接口地址不存在, apiId={}", api.getApiId());
            throw WarnExceptionUtils.newInstance("接口地址不存在");
        }

        if (!savedApi.getApiCode().equals(api.getApiCode())) {
            // 和原来不一致重新检查唯一性
            if (this.isExist(api.getApiCode())) {
                log.warn("接口地址已存在, apiCode={}", api.getApiCode());
                throw WarnExceptionUtils.newInstance(String.format("接口地址编码已存在，apiCode=%s", api.getApiCode()));
            }
        }

        if (null == api.getPriority()) {
            api.setPriority(0);
        }

        if (null == api.getApiCategory()) {
            api.setApiCategory(SysApiCategoryEnum.DEFAULT);
        }

        api.setUpdateTime(new Date());
        apiDao.update(api);
        // 同步权限表里的信息
        authorityService.saveOrUpdateAuthority(api.getApiId(), SysAuthorityTypeEnum.API);
    }

    @Override
    public Api getApi(Long apiId) {
        return apiDao.get(apiId);
    }

    @Override
    public Api getApi(String apiCode) {
        return apiDao.getByApiCode(apiCode);
    }

    @Override
    public Boolean isExist(String apiCode) {
        LambdaQueryWrapper<Api> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Api::getApiCode, apiCode);


        int count = this.apiDao.countByApiCode(apiCode);
        return count > 0;
    }

    @Override
    public List<Api> listByServiceId(String serviceId) {
        return apiDao.listByServiceId(serviceId);
    }

}
